
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<div class="modal" id="addLineDiv" tabindex="-1" role="dialog" aria-hidden="true">

	<div class="modal-dialog" role="document">

		<div class="modal-content animated bounceInRight">

			<div class="modal-header">
				<h4 class="modal-title">工序转移/完成/退货单</h4>
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
			</div>

			<div class="modal-body" style="padding-bottom: 20px;">
				<%-- 导入提示信息框 --%>
			    <c:if test="${hint!=null&&hint!=''}">
			   		<jsp:include page="../../common/alert/alert.jsp">
			   			<jsp:param value="${hint}" name="alertType"/>
			   			<jsp:param value="${alertMessage}" name="alertMessage"/>
			   		</jsp:include>
			    </c:if>
			
				<form id="cform">
					<div class="form-group row">
						<label class="col-sm-3 col-form-label"><span class="text-danger">*</span>单据类型</label>
						<div class="col-sm-9">
							<%-- 
							<select class="form-control" id="procedureShiftType" name="procedureShiftType">
	                        	<option value="" selected="selected">请选择...</option>
	                        	<c:forEach items="${requestScope.procedureShiftTypeMap}" var="procedureShiftType">
	                        		<option value="${procedureShiftType.key}">${procedureShiftType.value}</option>
	                        	</c:forEach>
	                        </select>
	                        --%>
	                        <div class="i-checks" style="margin-top: 5px;">
	                        	<c:forEach items="${requestScope.procedureShiftTypeMap}" var="procedureShiftType">
	                        		<c:if test="${procedureShiftType.key==requestScope.prodWipTaskProcedure.procedureShiftType}">
	                        			<label><input type="radio" value="${procedureShiftType.key}" name="procedureShiftType" checked="checked"><i></i> ${procedureShiftType.value}</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	                        		</c:if>
	                        		<c:if test="${procedureShiftType.key!=requestScope.prodWipTaskProcedure.procedureShiftType}">
	                        			<label><input type="radio" value="${procedureShiftType.key}" name="procedureShiftType" disabled="disabled"><i></i> ${procedureShiftType.value}</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	                        		</c:if>
	                        	</c:forEach>
                            </div>
						</div>
					</div>
					<div class="hr-line-dashed"></div>
				
					<div class="form-group row">
						<label class="col-sm-2 col-form-label"><span class="text-danger">*</span>自工序</label>
						<div class="col-sm-4">
							<input id="procedureName" name="procedureName" type="text" class="form-control" value="${requestScope.prodWipTaskProcedure.procedureName}" readonly="readonly">
							<input id="procedureCode" name="procedureCode" type="hidden" class="form-control" value="${requestScope.prodWipTaskProcedure.procedureCode}" readonly="readonly">
						</div>
					
						<label class="col-sm-2 col-form-label"><span class="text-danger">*</span>至工序</label>
						<div class="col-sm-4">
							<input id="shiftProcedureName" name="shiftProcedureName" type="text" class="form-control" value="${requestScope.prodWipTaskProcedure.shiftProcedureName}" readonly="readonly">
							<input id="shiftProcedureCode" name="shiftProcedureCode" type="hidden" class="form-control" value="${requestScope.prodWipTaskProcedure.shiftProcedureCode}" readonly="readonly">
						</div>
					</div>
					<div class="hr-line-dashed"></div>
					
					

					<div class="form-group row">
						<label class="col-sm-2 col-form-label"><span class="text-danger">*</span>物料</label>
						<div class="col-sm-4">
							<input id="procedureMaterialCode" type="text" class="form-control" readonly="readonly">
						</div>

						<label class="col-sm-2 col-form-label">单位</label>
						<div class="col-sm-4">
							<input id="procedureMaterialUnit" type="text" class="form-control" readonly="readonly">
						</div>
					</div>
					<div class="hr-line-dashed"></div>
					
					<div class="form-group row">
						<label class="col-sm-3 col-form-label"><span class="text-danger">*</span>数量</label>
						<div class="col-sm-9">
							<input id="finishQuantity" name="finishQuantity" type="text" class="form-control" value="${requestScope.prodWipTaskProcedure.finishQuantity}">
						</div>
					</div>
					<div class="hr-line-dashed"></div>
					
					<div class="form-group row">
						<label class="col-sm-3 col-form-label"><span class="text-danger">*</span>工时（天）</label>
						<div class="col-sm-9">
							<input id="finishDays" name="finishDays" type="text" class="form-control" value="${requestScope.prodWipTaskProcedure.finishDays}">
						</div>
					</div>
					<div class="hr-line-dashed"></div>
					
					<div class="form-group row" style="margin-bottom: 0px;">
						<label class="col-sm-3 col-form-label"><span class="text-danger">*</span>资源成本</label>
						<div class="col-sm-9">
							<input id="costAmount" name="costAmount" type="text" class="form-control" value="${requestScope.prodWipTaskProcedure.costAmount}">
							<span id="costAmountHint" class="help-block m-b-none" style="color: silver; font-size: 12px;"></span>
						</div>
					</div>
					<div class="hr-line-dashed" style="margin-top: 0px;"></div>

					<div class="form-group row">
						<label class="col-sm-3 col-form-label"><span class="text-danger">*</span>工序状态</label>
						<div class="col-sm-9">
							<select class="form-control m-b" name="procedureStatus" id="procedureStatus">
								<c:forEach items="${requestScope.taskProcedureStatusMap}" var="taskProcedureStatus">
	                        		<option value="${taskProcedureStatus.key}">${taskProcedureStatus.value}</option>
	                        	</c:forEach>
							</select>
						</div>
					</div>
					<div class="hr-line-dashed"></div>

					<div class="form-group row m-b-none">
						<div class="col-sm-12 col-sm-offset-2 text-right">
							<button class="btn btn-white btn-lg" type="button"
								data-dismiss="modal">返回</button>
							&nbsp;
							<button
								class="ladda-button ladda-button-demo btn btn-primary btn-lg"
								data-style="expand-right">
								&nbsp;&nbsp;确定&nbsp;&nbsp;<i class="fa fa-check-square-o"></i>
							</button>
						</div>
					</div>
					
					<input type="hidden" id="taskCode" name="taskCode" value="${param.taskCode}"> 
					<input type="hidden" id="taskProcedureId" name="taskProcedureId" value="${requestScope.prodWipTaskProcedure.taskProcedureId}"> 
					<input type="hidden" id="taskProcedureCode" name="taskProcedureCode" value="${requestScope.prodWipTaskProcedure.taskProcedureCode}"> 
					<input type="hidden" id="workCenterCode" name="workCenterCode" value="${requestScope.prodWipTaskProcedure.workCenterCode}"> 
					<input type="hidden" id="createdDate" name="createdDate" value="${requestScope.prodWipTaskProcedure.createdDate}"> 
					<input type="hidden" id="createdBy" name="createdBy" value="${requestScope.prodWipTaskProcedure.createdBy}">
				</form>
			</div>

		</div>

	</div>

</div>

<script>
	$(document).ready(function() {
		//初始化物料
		$("#procedureMaterialCode").val($("#materialCode").find("option:selected").text());
		//初始化物料单位
		$("#procedureMaterialUnit").val($("#materialUnit").find("option:selected").text());
		//初始化status
		if("${requestScope.prodWipTaskProcedure.status}"!=""){
			$("#procedureStatus").val("${requestScope.prodWipTaskProcedure.status}");
		}
		
		//单据类型效果
		$("#procedureStatus").click(function(){
			redragonJS.alert("单据状态不能修改");
		});
		
		//初始化radio
		$('.i-checks').iCheck({
            //checkboxClass: 'icheckbox_square-green',
            radioClass: 'iradio_square-green',
        });
        
        //根据数量计算成本金额
        $("#finishQuantity").blur(function(){
        	getProcedureResourceCostAjax($("#procedureCode").val(), $("#finishQuantity").val());
        });
        
        //初始化退货时的效果
        if($(":radio[name='procedureShiftType']:checked").val()=="RETURN"){
        	$("#finishDays").prop("disabled", true);
        	$("#costAmount").prop("disabled", true);
        }
        

		
		//表单提交
		var l = $('.ladda-button-demo').ladda();

		l.click(function() {
			$("#cform").valid();
			//l.ladda('stop');
		});

		$("#cform").validate({
			rules : {
				procedureShiftType : {
					required : true,
				},
				procedureCode : {
					required : true,
				},
				finishQuantity : {
					required : true,
				},
				finishDays : {
					required : true,
				},
				costAmount : {
					required : true,
				},
				procedureStatus : {
					required : true,
				},
			},
			submitHandler: function(form) {
				var submitFlag = "Y";
				//验证字段
				if($("#procedureShiftType").val()=="SHIFT"){
					if($("#shiftProcedureCode").val()==""){
						submitFlag = "N";
						redragonJS.alert("工序转移单的转移工序必填");
					}
				}
				
				if(submitFlag=="Y"){
					l.ladda('start');
					editLine();
				}
		    }
		});
		
	});
	
	//异步编辑行
	function editLine(){
		redragonJS.loading("line-ibox-content");
		//获取单据类型
		var procedureShiftType = $(":radio[name='procedureShiftType']:checked").val();
		
		$.ajax({
			type: "post",
			url: "web/prodWipTaskProcedure/editProdWipTaskProcedure",
			data: {"taskProcedureId": $("#taskProcedureId").val(), "costAmount": $("#costAmount").val(), "finishDays": $("#finishDays").val(), 
				   "status": $("#procedureStatus").val(), "finishQuantity": $("#finishQuantity").val(), "procedureCode": $("#procedureCode").val(),
				   "shiftProcedureCode": $("#shiftProcedureCode").val(), "workCenterCode": $("#workCenterCode").val(), "taskCode": $("#taskCode").val(),
				   "taskProcedureCode": $("#taskProcedureCode").val(), "createdDate": $("#createdDate").val(), "createdBy": $("#createdBy").val(),
				   "procedureShiftType": procedureShiftType},
			async: false,
			dataType: "json",
			cache: false,
			success: function(data){
				redragonJS.removeLoading("line-ibox-content");
				$('#addLineDiv').modal('hide');
				$('.ladda-button-demo').ladda('stop');
				
				if(data.result=="success"){
					if(data.message=="成功"){
						if(data.refresh=="Y"){
							window.location.reload();
						}else{
							getProcedureLineTab($("#taskCode").val());
						}
					}else{
						redragonJS.alert(data.message);
					}
					
				}else{
					redragonJS.alert("工序转移错误");
				}
				
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				redragonJS.alert(textStatus);
			}
		});
	}
	
	//获取当前工序资源的成本
	function getProcedureResourceCostAjax(procedureCode, finishQuantity){
		$.ajax({
			type: "post",
			url: "web/prodWipTaskProcedure/getProcedureResourceCostAjax",
			data: {"procedureCode": procedureCode, "finishQuantity": finishQuantity},
			async: false,
			dataType: "json",
			cache: false,
			success: function(data){
				if(data!=""){
					$("#costAmount").val(data.costAmount);
					$("#costAmountHint").html(data.costHint);
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				redragonJS.alert(textStatus);
			}
		});
	}
</script>
